<template>
	<view class="body">
		<xk-header :headerbg="true" :back="true" title="意见箱"></xk-header>

		<view class="tabs-bar" scroll-x="true">
			<view v-for="(item, index) of tabs" :key="index" :class="tabIndex === index ? 'item item-a' : 'item'" @click="clickTab(index)" >{{item.title}}</view>
		</view>
		<view class="list-info">
			<swiper @change="listInfoChange" :current="tabIndex" class="list-swiper"
				:style="{height: scrollViewHeight + 'px'}">
				<swiper-item v-for="(tabItem, tabIndex) of tabs" :key="tabIndex">
					<scroll-view class="data-list" scroll-y="true"
						refresher-enabled="true" :refresher-triggered="tabs[tabIndex].triggered" :refresher-threshold="100"
						@scrolltolower="onScrollToLower()"  @refresherrefresh="onRefresh()">
						<view class="item-view">
							<view class="search-view">
								<picker mode="date" :value="dates[tabIndex].date" fields="month"
								 @change="dateChange">
									<view class="date-view">
										<view>{{dates[tabIndex].date || "全部日期"}}</view>
										<image mode="aspectFill" class="xiala" :src="(imgUrl+'/wx/xiala.png')"/>
									</view>
								</picker>

								<view class="date-view" @click="resetSort()">
									<view>重置</view>
								</view>
							</view>
							<view  v-if="dataList[tabIndex] && dataList[tabIndex].length > 0">
								<view class="data-item" v-for="(item, index) of dataList[tabIndex]" :key="index">
									<!--  -->
									<view class="title">
										<view>意见箱</view>
										<view :class="item.status==='HANDLE' ? 'status' : ''">{{$dicUtil.FeedbackStatus(item.status)}}</view>
									</view>
									<view class="info">
										<view class="name">反馈业主：</view>
										<view class="desc">{{item.apply_user}}</view>
									</view>
									<view class="info">
										<view class="name">反馈类型：</view>
										<view class="desc">{{types[item.fb_type]}}</view>
									</view>
									<!-- <view class="info">
										<view class="name">反馈设施：</view>
										<view class="desc">{{item.device}}</view>
									</view> -->
									<view class="info">
										<view class="name">反馈时间：</view>
										<view class="desc">{{item.apply_date}}</view>
									</view>
									<view class="info">
										<view class="name">反馈描述：</view>
										<view class="desc">
											<view>{{item.fb_desc}}</view>
											<image-list v-if="item.fb_pic" :pics="item.fb_pic"></image-list>
											<!-- <view class="desc-img">
												<image mode="scaleToFill" v-for="(imgItem, imgIndex) of item.fb_images" :key="imgIndex"
													:src="imgItem | formatImgPath" @click="imageClick(item.fb_images, imgIndex)"/>
											</view> -->
										</view>
									</view>

									<!-- <view class="info bottom-info" v-if="tabIndex===0">
										<view class="cancel" @click="onEdit(item)">编辑</view>
										<view class="submit" @click="onDeleteDrap(item)">删除</view>
									</view> -->

									<view class="info bottom-info" v-if="item.status == 'ACCEPT' || item.status == 'DRAFT'">
										<view  class="cancel" @click="onCancel(item)">撤销申请</view>
									</view>
									<view class="info bottom-info" v-if="item.status == 'EVALUATE'">
										<view class="submit" @click="onEvaluate(item)" >评价服务</view>
									</view>

									<view class="bottom-info-list" v-if="tabIndex===1">
										<view class="title">反馈受理进度</view>
										<view class="info">
											<view class="name">接单人：</view>
											<view class="desc">{{item.deal_user || ""}}</view>
										</view>
										<view class="info">
											<view class="name">处理时间：</view>
											<view class="desc">{{item.deal_time || ""}}</view>
										</view>
										<view class="info">
											<view class="name">处理状态：</view>
											<view class="status">{{item.status==="COMPLETE" ? "已完成" : "处理中"}}</view>
										</view>
										<view class="info" v-if="tabIndex===1">
											<view class="name">处理结果：</view>
											<view class="desc">
												<view>已打扫干净</view>
												<image-list v-if="item.deal_pic" :pics="item.deal_pic"></image-list>
												<!-- <view class="desc-img">
													<image mode="scaleToFill" v-for="(imgItem, iIndex) of item.deal_pics" :key="iIndex"
														:src="imgItem | formatImgPath"/>
												</view> -->
											</view>
										</view>
										<view class="info info-btn" v-if="tabIndex===1">
											<!-- <view class="common-btn btn-sty2"
												v-if="item.evaluate_result">已评价</view> -->
										</view>
									</view>
								</view>
							</view>
						</view>

						<view class="empty-view" v-if="!dataList[tabIndex] || dataList[tabIndex] <= 0">
							<image class="empty-icon" :src="(imgUrl+'/wx/empty_data_icon.png')"></image>
							<view>哎呀，该界面暂无内容~</view>
						</view>
					</scroll-view>

				</swiper-item>
			</swiper>
		</view>
		<view class="bottom-view">
			<view class="common-btn" @click="onSubmit()">发起反馈</view>
		</view>
	</view>
</template>

<script>
	import {formatImgPath} from '../../common/filter.js'
	import moment from 'moment'
	export default {
		data() {
			return {
				imgUrl:this.$config.BASE_imgUrl,
				scrollViewHeight: 0,
				tabIndex: 0,
				tabs: [{
					title: "待处理",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				},{
					title: "已完成",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				}],
				types: {},
				dates: [{
					date: "" //moment().format("YYYY-MM"),
				},{
					date: "" //moment().format("YYYY-MM"),
				}],
				dataList: [[], []]
			};
		},
		onLoad(){
			this.initInfo();
		},
		onShow() {
			this.$util.getHeightWithTopEl(".tabs-bar").then(height => {
				this.$util.getElRect(".bottom-view").then(rect => {
					console.warn("aaa: ", rect, height)
					this.scrollViewHeight = height - rect.height;
					console.warn("height: ", this.scrollViewHeight)
				});
			});

			if (this.tabIndex === 0) {
				this.queryList()
			}
		},
		methods:{
			initInfo() {
				this.$api.baseData_getToolDataList({code:'PM_FEEDBACK_FBTYPE' + '_nostr'}).then(res => {
					console.warn("反馈类型：", res)
					for (let item of res) {
						this.types[item.data_value] = item.data_text;
					}
				})

				// 设置所有加载标识
				for (let index in this.tabs) {
					this.tabs[index].isLoad = true
				}
				this.queryList();
			},
			queryList() {
				// 判断是否重新加载，isReload为true时，清空对应的列表数据
				for (let index in this.tabs) {
					let tab = this.tabs[index]
					if (tab.isReload) {	// 重新加载数据
						this.dataList[index].splice(0, this.dataList[index].length);
						this.tabs[index].isReload = false
						this.tabs[index].start = 0
						this.tabs[index].isLoad = true
					}
				}
				let params = {
					start: this.tabs[this.tabIndex].start,
					limit: 20
				}
				if (this.dates[this.tabIndex].date) {
					params.dateMonth = this.dates[this.tabIndex].date
				}
				this.$api.pmFeedback_getPmFeedbackList(params).then(res=>{
					console.warn("反馈列表查询结果：", res)

					if (this.tabs[0].isLoad) {
						for (let item of res.unDoneFeedbackList) {
							// 处理报修图片
							if (item.fb_pic) {
								item.fb_images = []
								let fb_pics = item.fb_pic.split(";");
								for (let imgItem of fb_pics) {
									if (!imgItem) continue;
									item.fb_images.push(imgItem);
								}
							}

							let deal_pics = []
							if (item.deal_pic) {	// 处理图片
								for (let imgItem of item.deal_pic.split(";")) {
									if (!imgItem) continue;
									deal_pics.push(imgItem)
								}
							}
							item["deal_pics"] = deal_pics

							this.dataList[0].push(item)
						}
						this.tabs[0].start += res.unDoneFeedbackList.length
					}

					if (this.tabs[1].isLoad) {
						for (let item of res.completeFeedbackList) {	//已完成
							// 处理报修图片
							if (item.fb_pic) {
								item.fb_images = []
								let fb_pics = item.fb_pic.split(";");
								for (let imgItem of fb_pics) {
									if (!imgItem) continue;
									item.fb_images.push(imgItem);
								}
							}

							let deal_pics = []
							if (item.deal_pic) {	// 处理图片
								for (let imgItem of item.deal_pic.split(";")) {
									if (!imgItem) continue;
									deal_pics.push(imgItem)
								}
							}
							item["deal_pics"] = deal_pics

							this.dataList[1].push(item)
						}
						this.tabs[1].start += res.completeFeedbackList.length
					}

					// 设置所有加载标识
					for (let index in this.tabs) {
						this.tabs[index].isLoad = false

						setTimeout(() => {
							this.tabs[index].triggered = false
						}, 1000)
					}
				})
			},
			clickTab(index){
				this.tabIndex = index
			},
			resetSort() {
				let isReload = false
				if (this.dates[this.tabIndex].date) {
					this.dates[this.tabIndex].date = ""
					isReload = true
				}

				if (isReload) {
					// 设置数据加载标识为重新加载
					this.tabs[this.tabIndex].isReload = true
					this.queryList()
				}
			},
			dateChange(e) {
				if (this.dates[this.tabIndex].date !== e.detail.value) {
					this.dates[this.tabIndex].date = e.detail.value
					// 设置数据加载标识为重新加载
					this.tabs[this.tabIndex].isReload = true
					this.queryList()
				}
			},
			listInfoChange(e){
				this.tabIndex = parseInt(e.target.current)
			},
			imageClick(images, index) {
				let imgUrls = []
				for (let image of images) {
					imgUrls.push(formatImgPath(image))
				}
				uni.previewImage({
					current: index,
					urls: imgUrls
				})
			},
			onCancel(item) {
				this.$util.confirm({
					content: "确定要撤消反馈信息？",
					confirmText: "撤消"
				}).then(isConfirm => {
					if (isConfirm) {
						this.$util.post({
							url: "wx/pmFeedback/cancel",
							data: {
								pm_feedback_id: item.pm_feedback_id,
								cancel_reason: ""
							}
						}).then((res)=>{
							console.warn("撤消结果：", res);
							this.dataList[this.tabIndex].splice(this.dataList[this.tabIndex].indexOf(item), 1)
						});
					}
				})
			},
			onSubmit(item) {
				let eventName = "feedback-index";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, item => {
					this.$EventBus.$off(eventName)
					// 设置数据加载标识为重新加载
					this.tabs[0].isReload = true
					this.queryList()
				})
				let feedBackType = []
				for(let item of this.dataList[0]){
					if(item.status == 'ACCEPT'){
						feedBackType.push(item.fb_type)
					}
				}
				uni.navigateTo({
					url: "/pagesA/feedback/feedback-form?isListIn=true&eventName="+eventName + '&feedBackType='+ JSON.stringify(feedBackType)
				})
			},
			reloadData() {
				for (let index in this.tabs) {
					this.tabs[index].isReload = true;
				}
				this.queryList();
			},
			onEvaluate(item) {
				let eventName = "feedback-index";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, itemTemp => {
					this.$EventBus.$off(eventName)
					this.reloadData()
					// let index = this.dataList[this.tabIndex].indexOf(item)
					// this.dataList[this.tabIndex][index].evaluate_result = itemTemp.evaluate_result
					// this.dataList[this.tabIndex][index].evaluate_content = itemTemp.evaluate_content
				})

				uni.navigateTo({
					url: "/pagesA/feedback/feedback-evaluate?data="+JSON.stringify(item)+"&eventName="+eventName
				})
			},
			onScrollToLower(e) {
				if (this.tabs[this.tabIndex].isLoad) return
				this.tabs[this.tabIndex].isLoad = true
				this.queryList()
			},
			onRefresh() {
				console.warn("sdfsdfsdf: ", this.tabs[this.tabIndex].isLoad)
				if (this.tabs[this.tabIndex].isLoad) return
				this.tabs[this.tabIndex].isReload = true
				this.tabs[this.tabIndex].triggered = true
				this.queryList()
			},
		}
	}
</script>

<style lang="scss" scoped>

.tabs-bar{
	display: flex;
	align-items: center;
	flex-direction: row;
	justify-content: space-around;
	padding: 20rpx 30rpx 0rpx 30rpx;
	background-color: #FFFFFF;
	white-space: nowrap;
	.item{
		align-items: center;
		justify-content: center;
		padding-bottom: 20rpx;
		font-size: 32rpx;
		color: #333333;
		border-bottom: 6rpx solid rgba(#2C72FF, 0);
	}
	.item-a {
		font-weight: bolder;
		border-bottom: 6rpx solid #2C72FF;
	}
}

.list-info {
	.list-swiper {
		height: 1000rpx;
	}
	.data-list{
		height: 100%;

		.search-view {
			display: flex;
		}

		.item-view {

			padding: 0rpx 30rpx;
		}

		.date-view {
			display: flex;
			align-items: center;
			margin-top: 30rpx;
			margin-right: 50rpx;
			font-size: 28rpx;
			font-weight: bold;
			color: #333;

			.xiala {
				width: 21rpx;
				height: 13rpx;
				margin-left: 10rpx;
			}
		}
		.data-item{
			display: flex;
			flex: 1;
			margin-top: 20rpx;
			border-radius: 10rpx;
			padding: 20rpx;
			background-color: #ffffff;
			flex-direction: column;
			.img{
				width: 335rpx;
				height: 250rpx;
				border-radius: 10rpx;
			}

			.title{
				display: flex;
				flex-direction: row;
				font-size: 32rpx;
				font-weight: bolder;
				color: #333333;
				margin-bottom: 20rpx;

				:first-child {
					flex: 1;
				}
				:last-child {
					padding: 5rpx 15rpx;
					font-size: 24rpx;
					color: #2C72FF ;
					background-color: rgba($color: #A5C2FF, $alpha: 0.16);
					border-radius: 5rpx;
				}

				.status {
					color: #FF6400;
					background-color: rgba($color: #FFCDA5, $alpha: 0.16);
				}
			}

			.info{
				display: flex;
				font-size: 28rpx;
				color: #333;
				margin-top: 10rpx;

				.name {
					color: #999;
					width: 150rpx;
				}
				.cancel {
					font-size: 32rpx;
					font-weight: bold;
				}

				.submit {
					font-size: 32rpx;
					font-weight: bold;
					color: #0055FE;
				}

				.desc {
					flex: 1;
				}


				.desc-img {
					display: flex;
					flex-wrap: wrap;
					margin-top: 10rpx;
					>image {
						width: 120rpx;
						height: 120rpx;
						margin: 0rpx 5rpx 5rpx 0rpx;
					}
				}

				.status {
					color: #2C72FF;
				}
			}
			.info-btn {
				display: flex;
				justify-content: flex-end;
				>view {
					width: 240rpx;
					margin-bottom: 0rpx;
				}
			}
			.bottom-info {
				margin-top: 20rpx;
				padding-top: 20rpx;
				justify-content: space-around;
				border-top: 1rpx #F5F5F5 solid;
			}
			.bottom-info-list {
				margin-top: 20rpx;
				padding-top: 20rpx;
				border-top: 1rpx #F5F5F5 solid;
			}
		}
	}
}

.bottom-view {
	position: fixed;
	width: 750rpx;
	bottom: var(--window-bottom);
}
</style>
